package 剑指专题.链表;


/*
 * Author：江松
 * Date：2023/3/26 12:00

 删除链表的节点：直接比较删除即可,需要借助辅助头，因为可能删头节点

 删除链表中重复的节点：

 */

public class Main7 {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }


    public class Solution {
        public ListNode deleteDuplication(ListNode pHead) {
            ListNode head=new ListNode(0);
            head.next=pHead;
            ListNode pre=head,now=head.next;
            boolean flag=false;//是否有重复
            while(now!=null){
                while(now.next!=null&&now.val==now.next.val){
                    now=now.next;
                    flag=true;
                }
                if(flag){
                    now=now.next;
                    pre.next=now;
                }else{
                    now=now.next;
                    pre=pre.next;
                }
                flag=false;
            }
            return head.next;
        }
    }

    public ListNode deleteNode (ListNode head, int val) {
        ListNode newHead=new ListNode(0);
        newHead.next=head;
        ListNode p=newHead;
        while(p.next!=null){
            if(p.next.val==val){
                p.next=p.next.next;
            }
            p=p.next;
        }
        return newHead.next;
    }

}
